//
//  TTExistGoodsTableViewCell.m
//  TaoTuo
//
//  Created by apple on 2020/11/26.
//  Copyright © 2020 刘广. All rights reserved.
//

#import "TTExistGoodsTableViewCell.h"

@interface TTExistGoodsTableViewCell ()

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UILabel *leftBottomLabel;
@property (nonatomic, strong) UILabel *rightBottomLabel;

@end

@implementation TTExistGoodsTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupSubviews];
        
        
    }
    return self;
}

- (void)setModel:(TTDetailStockListModel *)model
{
    _model = model;
    
    // 行间距
    NSString *contentStr = [model.GoodsAttr componentsJoinedByString:@"\n"];
    NSMutableAttributedString *contentAttr = [[NSMutableAttributedString alloc] initWithString:contentStr];
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.lineSpacing = 5;
    [contentAttr addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, contentStr.length)];
    
    NSString *categoryName = [contentStr componentsSeparatedByString:@"-"].firstObject;
    if (categoryName.length > 0) {
        [contentAttr addAttributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:13], NSForegroundColorAttributeName : UIColorHex(#8B57E6)} range:[contentStr rangeOfString:categoryName]];
    }
    self.contentLabel.attributedText = contentAttr;
    
    
    
    self.leftBottomLabel.text = model.FactoryLabel;
    
//    [self.locationBtn setTitle:listModel.LocationName forState:UIControlStateNormal];
    
//    NSString *preStr = model.GoodsValuationPriceTypeText.length > 0 ? model.GoodsValuationPriceTypeText : @"";
//    NSString *cha = model.PriceDiffRangText ? : @"";
//    NSString *price = [NSString stringWithFormat:@"%@￥%@%@", preStr, model.Price, cha];
    NSString *price = [NSString stringWithFormat:@"¥%@", model.Price];
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:price];
    [attr addAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} range:[price rangeOfString:model.Price]];
    self.priceLabel.attributedText = attr;
    
    self.rightBottomLabel.text = @"现货";
    
    UIColor *borderColor = model.selected ? UIColorHex(#AA7AFD) : UIColorHex(ffffff);
    UIColor *bgColor = model.selected ? UIColorHex(ffffff) : UIColorHex(#F6F6F6);;
    self.bgView.layer.borderColor = borderColor.CGColor;
    self.bgView.backgroundColor = bgColor;
    
}

- (void)setupSubviewFrames
{
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.mas_equalTo(15);
        make.right.equalTo(self.contentView.mas_right).offset(-15);
        make.bottom.equalTo(self.contentView);
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(8);
        make.left.mas_equalTo(10);
        make.right.equalTo(self.bgView.mas_right).offset(-50);
    }];
    
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.bgView.mas_right).offset(-10);
        make.bottom.equalTo(self.contentLabel);
    }];
    
    [self.leftBottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentLabel.mas_bottom).offset(10);
        make.left.equalTo(self.contentLabel);
    }];
    
    [self.rightBottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.priceLabel.mas_bottom).offset(10);
        make.right.equalTo(self.priceLabel);
        make.bottom.equalTo(self.bgView).offset(-10);
    }];
    
}

- (void)setupSubviews
{
    _bgView = [[UIView alloc] init];
    _bgView.backgroundColor = UIColorHex(#F6F6F6);
    _bgView.layer.cornerRadius = 10;
    _bgView.layer.borderWidth = 0.5;
    [self.contentView addSubview:_bgView];
    
    _contentLabel = [UILabel labelWithTitle:@"" color:UIColorHex(333333) fontSize:13];
    _contentLabel.numberOfLines = 0;
    [self.bgView addSubview:_contentLabel];
    
    _priceLabel = [UILabel labelWithTitle:@"" color:UIColorHex(#FF1F1F) fontSize:15];
    [self.bgView addSubview:_priceLabel];
    
    _leftBottomLabel = [UILabel labelWithTitle:@"" color:UIColorHex(#AA7AFD) fontSize:12];
    [self.bgView addSubview:_leftBottomLabel];
    
    _rightBottomLabel = [UILabel labelWithTitle:@"" color:UIColorHex(#AA7AFD) fontSize:12];
    [self.bgView addSubview:_rightBottomLabel];
    
    
    [self setupSubviewFrames];
    
}


@end
